<?php
namespace app\interfaces\controller;

use app\interfaces\model\GoodsType;
use think\Db;
use think\Cache;
use think\Request;
use app\interfaces\model\Ad as AdModel;
use app\interfaces\model\Shop as ShopModel;
use app\interfaces\model\Config as ConfigModel;
use app\interfaces\model\QuotHour as QuotHourModel;
use app\interfaces\model\GoodsType as GoodsTypeModel;
use app\interfaces\model\ShopGoods as ShopGoodsModel;
use app\interfaces\model\Promotion as PromotionModel;
use think\View;


class Index
{

    /*
     * 用户端 - 首页
     */
    public function index()
    {
return 1;
        $param = Request::instance()->param();
        // banner列表
        $banner_list = AdModel::field('id,linkUrl,picUrl,skip_type')->where('status',1)->select();

        // 促销活动
        $pro_list = PromotionModel::where(['status' => '1'])
            ->field('id,title,goods_id')
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->limit(5)
            ->where('status',1)
            ->select();
        $type_list = (new GoodsType())->where(['status' => '1', 'count' => '2','recommend' => 1])
            ->field('id,title,sort')
            ->order(['sort' => 'asc','id' => 'asc'])->limit(8)->select()->toArray();
//        $condition = [];
//        if(isset($param['title']) && !empty($param['title'])) $condition['title'] = ['like','%'.$param['title'].'%'];
        $goods_list1 = ShopGoodsModel::where(['state' => 1])
            ->field('id,title,price,thumb_img,goods_attr')
            ->order(['sort' => 'asc','id' => 'desc'])
            ->limit(0,5)
            ->select()->toArray();
        foreach ($goods_list1 as $k1 => &$v1){
            $v1['type'] = 0;
        }
        $type = [['type' => 1,'type_list' => $type_list]];
        $goods_list = array_merge($goods_list1,$type);

        $goods_list_11 = array_merge($goods_list1,$type_list);
        $goods_list2 = ShopGoodsModel::where(['state' => 1])
            ->field('id,title,price,thumb_img,goods_attr')
            ->order(['sort' => 'asc','id' => 'desc'])
            ->limit(5,5)
            ->select()->toArray();
        foreach ($goods_list2 as $k2 => &$v2){
            $v2['type'] = 0;
        }
        $goods_list = array_merge($goods_list,$goods_list2);

        $member_message = Db::name('member_message')->select();
        $message_count = Db::name('member_message')->count();
        $user_id = Request::instance()->param('user_id','0');
        $no_read_message_count = Db::name('mem_mess')->where('member_id','eq',$user_id)->where('click',0)->count();
        if(empty($user_id)) $no_read_message_count = 0;
        $tel = ConfigModel::where('varname','web_customer_service')->value('varvalue');




        return [
            'code'         => 200,
            'message'      => '成功',
            'banner_list'  => $banner_list,
            'pro_list'     => $pro_list,
            'goods_list' => $goods_list,
            'no_read_message_count' => $no_read_message_count,
            'tel' => $tel,
        ];

    }

    public function logo()
    {
        $content = '<style> img {vertical-align:middle; width:auto;} </style>';
        $content .= '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">';
        $content .= '<img src="/logo176.png" style="width:100%;display:block" />';


        return response($content);
    }


    /**
 * 检查更新(安卓)
 */
    public function check_update()
    {

        $data = Request::instance()->param('versions');

        $versions = (int)Db::name('Config')->where('varname', 'web_android_version')->value('varvalue');

        $url = "https://www.pgyer.com/NcF9";
        //是否强制更新
        // if ($versions['force'] == 0) {

        if ($data == $versions) {
            return ['code' => 403, 'message' => '无需更新'];
        } else {
            return ['code' => 200, 'message' => '可以更新', 'url' => $url];
        }
        // } else {
        //     return ['code' => 200, 'message' => '强制更新', 'url' => $url];
        // }


    }


    public function ios_update()
    {
        $data = Request::instance()->param('versions');

        $versions = (float)Db::name('Config')->where('varname', 'web_ios_version')->value('varvalue');

        $url = "https://www.pgyer.com/7pjm";
        //是否强制更新
        // if ($versions['force'] == 0) {

        if ($data == $versions) {
            return ['code' => 403, 'message' => '无需更新','versions' => $versions];
        } else {
            return ['code' => 200, 'message' => '可以更新', 'url' => $url, 'versions' => $versions];
        }
        // } else {
        //     return ['code' => 200, 'message' => '强制更新', 'url' => $url];
        // }
    }


}
